package com.vast.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vast.dao.InformationDao;
import com.vast.entity.InformationEntity;
import com.vast.pojo.Result;
import com.vast.pojo.StatusCode;
import com.vast.service.InformationService;
import com.vast.utils.IdWorker;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


@Service("informationService")
public class InformationServiceImpl  implements InformationService {
    @Resource
    InformationDao informationDao;

    @Override
    public void save(InformationEntity informationEntity) {
        informationEntity.setInfoId(getID());
        informationEntity.setInfoStatus(0);
        informationDao.insert(informationEntity);
    }

    @Override
    public InformationEntity getByProId(Integer productId,Integer userID) {
        QueryWrapper<InformationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id",productId);
        InformationEntity informationEntity = informationDao.selectOne(queryWrapper);
        return informationEntity;
    }


    @Override
    public Result getAll(InformationEntity informationEntity) {
        Page<InformationEntity> page = new Page<>(informationEntity.getPageNum(),informationEntity.getPageSize());
        QueryWrapper<InformationEntity> queryWrapper = new QueryWrapper<>();
        if(informationEntity.getContactName()!=null && !informationEntity.getContactName().equals("")){
            queryWrapper.like("contact_name",informationEntity.getContactName());
        }
        Page<InformationEntity> entityPage =informationDao.selectPage(page, queryWrapper);
        return new Result(StatusCode.OK,"贷款数据查询成功",true,entityPage);
    }

    @Override
    public InformationEntity getByInFoId(int infoId) {
        QueryWrapper<InformationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("info_id",infoId);
        InformationEntity informationEntity = informationDao.selectOne(queryWrapper);
        return informationEntity;
    }

    public  Integer getID() {
        IdWorker idWorker = new IdWorker();
        long l = idWorker.nextId();
        String str="";
        str = l +"";
        String substring = str.substring(0, 6);
        int i = Integer.parseInt(substring);
        return i;
    }
}